//
//  KhwAgreementController.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/1/15.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit
import WebKit
class ZyaAgreementVC: UIViewController,WKNavigationDelegate, WKUIDelegate {

    var webheight:CGFloat?
    var agreeView:UIView!
    var agreeBox:UIButton!
    var tbBtn:UIButton!
    var disMissClosure:((Bool)->())?
    var z_zb_id = ""

    
    static func initWithNav(z_zb_id:String,disMissClosure:((Bool)->())?) -> UIViewController {
        let nav = BaseNavigationController()
        let vc = ZyaAgreementVC()
        vc.z_zb_id = z_zb_id
        vc.disMissClosure = disMissClosure
        nav.viewControllers = [vc]
        return nav
    }
    
   
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.title = "\(APP_NAME)协议"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: .done, target: self, action: #selector(cancelAction))

        // Do any additional setup after loading the view.
        let webview = WKWebView(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: KSGlobal.screenH))
        webview.navigationDelegate = self
        webview.uiDelegate = self
        webview.scrollView.delegate = self
        self.view.addSubview(webview)
        let bView = UIView()
        self.view.addSubview(bView)
        webview.snp.makeConstraints { (make) in
            make.top.equalTo(self.view)
            make.left.equalTo(self.view)
            make.right.equalTo(self.view)
            make.bottom.equalTo(bView.snp.top)
        }
       
        bView.snp.makeConstraints { (make) in
             make.bottom.equalTo(self.view)
               make.left.equalTo(self.view)
               make.right.equalTo(self.view)
               make.height.equalTo(90)
        }
        
        tbBtn = UIButton()
        tbBtn.backgroundColor = .lightGray
        tbBtn.addTarget(self, action: #selector(tbAction(sender:)), for: .touchUpInside)
        tbBtn.setTitle("确认", for: .normal)
        bView.addSubview(tbBtn)
        tbBtn.snp.makeConstraints { (make) in
             make.centerX.equalTo(bView)
            make.bottom.equalTo(bView).offset(-10)
              make.width.equalTo(200)
              make.height.equalTo(45)
        }
        
        
        agreeView = UIView()
        bView.addSubview(agreeView)
        agreeView.isHidden = true
        agreeView.snp.makeConstraints { (make) in
            make.top.equalTo(bView)
             make.left.equalTo(bView)
            make.width.equalTo(KSGlobal.screenW)
               make.height.equalTo(25)
        }
        agreeBox = UIButton()
        agreeBox.setBackgroundImage(UIImage(named: "unselect_n"), for: .normal)
        agreeBox.setBackgroundImage(UIImage(named: "select_n"), for: .selected)
        agreeBox.addTarget(self, action: #selector(agreeAction(sender:)), for: .touchUpInside)
        agreeView.addSubview(agreeBox)
        agreeBox.snp.makeConstraints { (make) in
            make.top.equalTo(agreeView)
            make.left.equalTo(agreeView).offset(20)
            make.width.equalTo(agreeView.snp.height)
            make.height.equalTo(agreeView)
        }
        let agreeTip = UIButton()
        agreeTip.addTarget(self, action: #selector(agreeAction(sender:)), for: .touchUpInside)
        agreeTip.setTitleColor(#colorLiteral(red: 0.3253560066, green: 0.5377478004, blue: 1, alpha: 1), for: .normal)
        agreeTip.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        agreeTip.setTitle("我已认真阅读并同意《\(APP_NAME)协议》", for: .normal)
        agreeView.addSubview(agreeTip)
        agreeTip.snp.makeConstraints { (make) in
            make.centerY.equalTo(agreeView)
            make.left.equalTo(agreeBox.snp.right)
           make.width.equalTo(300)
           make.height.equalTo(agreeView)
        }
        
        if let url = URL(string: "http://package.zhuok.com.cn/packages/public/zya/protocol.html") {
           let  request = URLRequest(url: url)  
                webview.load(request)
             
        }else {
            KSToast("99")
        }
        
        
       
        
        
   
    }
    @objc func cancelAction() {
        self.dismiss(animated: true, completion: {
            if let clo = self.disMissClosure {
                clo(false)
            }
        })
  
    }
    
    @objc func agreeAction(sender:UIButton) {
        agreeBox.isSelected = !agreeBox.isSelected
        tbBtn.backgroundColor = agreeBox.isSelected ? #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1) : .lightGray
    }
    @objc func tbAction(sender:UIButton) {
        guard agreeBox.isSelected else {
            KSToast("请阅读本协议到最后一页，并同意本协议")
            return
        }
        userDefault.setValue("1", forKey: USERINFO.usrid.unwrap + z_zb_id)
        var dic = [String:String]()
        dic["z_user_ip"] = GetIPAddresses()
        dic["z_zb_id"] = z_zb_id
        dic["z_user_latitude"] = ""
        dic["z_user_longitude"] = ""
        dic["z_user_mac"] = GetIPAddresses()
        KSNetwork.shared.doRequest(API.saveOptInfo(data: dictionaryToJson(dic))) { (data:BaseResult<OperateResult>) in
            
        }
        
        
        self.dismiss(animated: true, completion: {
            if let clo = self.disMissClosure {
                clo(true)
            }
        })
        
 
        
   }
       
    
 

}
 
extension ZyaAgreementVC:UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
         
        if let wHeight = self.webheight {
            print(wHeight)
            print(scrollView.contentOffset.y)
            if wHeight - scrollView.contentOffset.y < KSGlobal.screenH {
                self.agreeView.isHidden = false
            } else {
                self.agreeView.isHidden = true
            }
        }
        
        
        
        
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.webheight = webView.scrollView.contentSize.height
    }
 
 
}
